package com.jian.异步FileChannle;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

/**
 * @author Jiange
 * @create 2021-09-14-9:22
 */
public class FileChannelDemo3 {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("C:\\Users\\lenovo\\Desktop\\jain.txt");
        //写数据的时候必须要文件存在否则报错
        AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        Future<Integer> future = fileChannel.read(byteBuffer, 0);

        while(!future.isDone()){
            byteBuffer.flip();
//            while (byteBuffer.remaining()>0){
//                System.out.println( byteBuffer.get());
//            }
            byte[] bytes = new byte[byteBuffer.limit()];
            byteBuffer.get(bytes);
            System.out.println(bytes.length);
            System.out.println(new String(bytes));
            byteBuffer.clear();
        }
    }
}
